package com.example.mypotiota_si.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mypotiota_si.Activity.LiaoTianActivity;
import com.example.mypotiota_si.Datas.Xiaoxi;
import com.example.mypotiota_si.R;

import java.util.ArrayList;

public class ListViewAdapter_xx extends RecyclerView.Adapter<ListViewAdapter_xx.ViewHolder> {
    public ArrayList<Xiaoxi> xiaoxi;
    Context context;

    public ListViewAdapter_xx(ArrayList<Xiaoxi> xiaoxi, Context context) {
        this.xiaoxi = xiaoxi;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=View.inflate(parent.getContext(),R.layout.item_xx_list_view,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.setData(xiaoxi.get(position));
        holder.xx_card_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, LiaoTianActivity.class);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (xiaoxi!=null){
            return xiaoxi.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView icon;
        private TextView name,nr;
        private CardView xx_card_view;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            icon=itemView.findViewById(R.id.xx_icon_tx);
            name=itemView.findViewById(R.id.xx_name_mz);
            nr=itemView.findViewById(R.id.xx_nr);
            xx_card_view=itemView.findViewById(R.id.xx_card_view);
        }

        public void setData(Xiaoxi xiaoxi) {
            icon.setImageResource(xiaoxi.xx_icon);
            name.setText(xiaoxi.xx_name);
            nr.setText(xiaoxi.xx_nr);
        }
    }
}
